/*
 * Copyright (c) 2015. For Intelligent Group.
 */

package com.intelligent.ispc.client.utils;

import java.io.*;
import java.util.List;

/**
 * Created by cherry on 2016/8/23.
 */
public class ArrayUtils {

    /**
     * Connect a string array to another string array
     *
     * @param a one string array
     * @param b another string array
     * @return connected string array
     */
    public static String[] concat(String[] a, String[] b) {
        String[] c = new String[a.length + b.length];
        System.arraycopy(a, 0, c, 0, a.length);
        System.arraycopy(b, 0, c, a.length, b.length);
        return c;
    }

    /**
     * A deep copy of the collection
     *
     * @param src source of collection
     * @param <T> type of class
     * @return copy of collection
     * @throws IOException            Read and write data when there is an exception
     * @throws ClassNotFoundException When classloader is loaded, it is found that the class does not exist in the class path.
     */
    public static <T> List<T> deepCopy(List<T> src) throws IOException, ClassNotFoundException {
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(byteOut);
        out.writeObject(src);

        ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
        ObjectInputStream in = new ObjectInputStream(byteIn);
        @SuppressWarnings("unchecked")
        List<T> dest = (List<T>) in.readObject();
        return dest;
    }
}
